<template>
	<Pages :current="2" class="tools">
		<CustomButton text="Update app" @click="onUpdate" backgroundColor="#409eff" />
		<CustomButton text="Logs" @click="onLog" backgroundColor="#67c23a" />
		<CustomButton text="Video" @click="onVideo" backgroundColor="#909399" />
		<CustomButton text="Song" @click="onSong" backgroundColor="#909399" />
		<CustomButton text="Image" @click="onImage" backgroundColor="#909399" />
		<CustomButton text="Clip" @click="onClip" backgroundColor="#909399" />
		<CustomButton text="Token" @click="onToken" backgroundColor="#909399" />
		<CustomButton text="Lib" @click="onLib" backgroundColor="#909399" />
		<CustomButton text="Lady" @click="onLady" backgroundColor="#909399" />
		<CustomButton text="Player" @click="onPlayer" backgroundColor="#909399" />
	</Pages>
</template>

<script setup>
	import CustomButton from '@/components/CustomButton.vue'
	import Pages from '@/components/pages/index.vue'
	import {
		useDebugStore
	} from '@/stores/debug'
	import {
		onMounted,
		ref
	} from 'vue'
	const debugStore = useDebugStore()
	const wgtUrl = "https://jsz315.xyz/apk/uni-record/mob.wgt?v=" + Math.random();

	const wgtVer = ref("1.0");

	// https://jsz315.xyz/mv-maker/index.html?app=uni-record

	function plusReady() {
		console.log("get info")
		// 获取本地应用资源版本号
		plus.runtime.getProperty(plus.runtime.appid, function(inf) {
			wgtVer.value = inf.version;
			debugStore.add("当前应用信息 inf", inf)
			console.log("当前应用版本：" + wgtVer);
			console.log("当前应用：" + inf);
		});
	}

	function onClip() {
		const url = 'https://jsz315.xyz/mv-maker/index.html';
		uni.navigateTo({
			url: "/pages/webs/webs?url=" + encodeURIComponent(url)
		})
		
		return
		// #ifdef APP
		plus.runtime.openURL(url, function(err) {
			if (err) {
				console.error('打开链接失败:', err);
				uni.showToast({
					title: '打开链接失败',
					icon: 'none',
				});
			}
		});
		// #endif
		
		// #ifdef H5
		window.open(url)
		// #endif
		
		
	}

	function onLog() {
		uni.navigateTo({
			url: "/pages/Log/Log"
		})
	}

	function onVideo() {
		uni.navigateTo({
			url: "/pages/videos/videos"
		})
	}

	function onPlayer() {
		uni.navigateTo({
			url: "/pages/Test/Player"
		})
	}

	function onSong() {
		uni.navigateTo({
			url: "/pages/Song/Song"
		})
	}

	function onImage() {
		uni.navigateTo({
			url: "/pages/imgs/imgs"
		})
	}

	function onLib(){
		uni.navigateTo({
			url: "/pages/Test/Libs"
		})
	}

	function onLady(){
		uni.navigateTo({
			url: "/pages/Test/Lady"
		})
	}

	function onToken() {
		uni.navigateTo({
			url: "/pages/settings/token"
		})
	}

	function toast(msg) {
		uni.showToast({
			icon: 'none',
			title: msg
		});
	}

	const onUpdate = () => {
		toast('Download...');
		debugStore.add("点击更新", null)
		uni.downloadFile({
			url: wgtUrl,
			success: (downloadResult) => {
				if (downloadResult.statusCode === 200) {
					// toast("Download success")
					debugStore.add("下载资源 downloadResult", downloadResult)
					plus.runtime.install(downloadResult.tempFilePath, {
						force: true
					}, function() {
						console.log('install success...');
						debugStore.add("安装成功", null)
						plus.runtime.restart();
					}, function(e) {
						debugStore.add("安装失败 e", e)
						console.error('install fail...');
					});
				}
			},
			fail: (err) => {
				toast('Download fail');
				debugStore.add("下载资源 err", err)
			}
		});
	}

	onMounted(() => {
		// #ifdef APP
		plusReady()
		// #endif
	})
</script>

<style lang="scss" scoped>
	:deep(.custom-button) {
		width: 100%;
	}

	.tools {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 20rpx;

		@supports not (gap: 30rpx) {
			margin-bottom: 30rpx;
		}
	}
</style>